<!--
 * @Author: your name
 * @Date: 2022-03-15 14:10:54
 * @LastEditTime: 2022-03-16 17:53:24
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \geyao\geyao\手写call220315.html
-->
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>手写callES6220316</title>
</head>
<body>
  <script>


  //传入两个参数 一个需要绑定的对象 一个传入的参数
  Function.prototype.mycall=function(obj,...args){
    //参数不是对象需要转成对象 当值为null喝undefined的时候默认指向windows
    obj = obj ? Object(obj) : window;
   
    //设置唯一值
    var key=Symbol()
     //修改this指向 添加一个属性指向this
     console.log(this,"this") //fn(a, b, c)
     console.log(args,"args") //fn(a, b, c)
     console.log(...args,"argsList") //fn(a, b, c)
    obj[key]=this
     //扩展运算符可以把字符串 转化为数组 数组转换为字符串
    var result=obj[key](...args)
    //删除已经绑定的对象属性
    delete obj.geyao
    return result
  }
    var name = 'fangfang';
		var obj = {
		    name: 'geyao'
		};

		function fn(a, b, c) {
		    console.log(a + b + c + this.name);
        return a + b + c + this.name
		};
    fn.mycall(obj,"我的", "老婆", "是"); // 我的老婆是geyao
  </script>
</body>
</html>